package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 新增地址薄
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    /**
     * 查询地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBookList = addressBookMapper.list(userId);
        return addressBookList;
    }

    /**
     * 根据地址薄id查询地址信息
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 根据地址薄id修改地址信息
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据地址薄id删除地址信息
     * @param id
     */
    @Override
    public void del(Long id) {
        addressBookMapper.del(id);
    }

    /**
     * 设置地址为默认地址
     * @param addressBook
     */
    @Override
    public void setDefult(AddressBook addressBook) {
        //先将所有地址薄设置为不是默认地址
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookMapper.list(addressBook.getUserId());

        addressBookMapper.setAllAddressBookNotDefault(list);

        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    /**
     * 查询默认地址信息
     * @return
     */
    @Override
    public AddressBook getDefault() {
        AddressBook addressBook = addressBookMapper.getDefault();
        return addressBook;
    }
}
